@extends('layouts.default')

@section('title', '班主任')

@push('scripts')
<script>
    layui.use(['layer','form','table','laytpl','laydate','element','laypage'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;

        renderSelect();

        laydate.render({
            elem: '#begin-date'
            ,type: 'date'
        });
        laydate.render({
            elem: '#end-date'
            ,type: 'date'
        });

        //下拉框初始化
        async function renderSelect(){
            await renderDictionarySelect();
            await renderTermSelect();
        }

        //字典下拉框数据渲染
        function renderDictionarySelect() {
            return new Promise((resolve) => {
                getDictionaryItem({'diccode': ['class_teacher', 'GRADE', 'class']}, function (res){
                    if(res.code === 1) {
                        let data = res.data;

                        for(var k in data) {
                            let key = k.toLowerCase();
                            let d = data[k];

                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                            for (var i in d) {
                                $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                            }
                        }

                        form.render();

                        resolve();
                    }
                });
            });
        }

        //学期下拉框数据渲染
        function renderTermSelect() {
            return new Promise((resolve) => {
                getTermData({},function(res){
                    if(res.code == 1){
                        $('[data-role=termList]').empty().append('<option value="">请选择</option>');
                        for(var i in res.data){
                            $('[data-role=termList]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                        }

                        form.render('select');

                        resolve();
                    }
                });
            });
        }

        form.on('select(gradeList)', function(data){
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象

            let gradeId = data.value;

            //renderClassSelect(gradeId);
        });

        function renderClassSelect(pid) {
            getDictionaryLinkItem({'parent_id': pid}, function (res){
                if(res.code === 1) {
                    let data = res.data;

                    for(var k in data) {
                        let key = k.toLowerCase();
                        let d = data[k];

                        $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                        for (var i in d) {
                            $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                        }
                    }

                    form.render();
                }
            });
        }

        //提交
        form.on('submit(sub)', function(data){
            let d = data.field;
            d.status = 1;
            addHeadmaster(d, function(res){
                if(res.code == '1'){
                    layer.msg(res.msg,{icon: 6,time: 1000},function(){
                        window.history.back();
                    });
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            });

            return false;
        });

        //保存
        $('#save-btn').click(function(){
            var isValid = form.validate('.data-version-id');
            if(!isValid) return false;

            var data = form.val("addForm");
            data.status = 2;

            addHeadmaster(data,function(res){
                if(res.code == '1'){
                    layer.msg(res.msg,{icon: 6,time: 1000},function(){
                        window.history.back();
                    });
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            });
            return false;
        });
    });
</script>
@endpush

@section('content')
<style>
    .opt-panel{padding: 10px;}
    .panel-title h3{margin-bottom: 20px;font-weight: 800;}

    .opt-form{padding: 20px 5px;}
    .opt-area{display: flex;flex-direction: row-reverse;padding: 10px 20px;margin-bottom: 20px;}
    .opt-area button, .back-btn{margin-left: 10px;}
</style>

<div class="layui-row">
    <div class="layui-panel opt-panel">
        <form class="layui-form opt-form" action="" id="add-form" lay-filter="addForm">
            <div class="layui-row">
                <div class="panel-title">
                    <h3>班主任 - 新增</h3>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>学年学期：</label>
                        <div class="layuik-input-inline">
                            <select class="data-version-id" name="data_version_id" lay-filter="termList" data-role="termList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>年级班级：</label>
                        <div class="layuik-input-inline" style="width: 40%">
                            <select name="grade_value" lay-filter="gradeList" data-role="gradeList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                        <div class="layuik-input-inline" style="width: 40%">
                            <select name="class_value" lay-filter="classList" data-role="classList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>开始日期：</label>
                        <div class="layuik-input-inline">
                            <input type="text" name="begin_date" id="begin-date" required lay-verify="required" placeholder="请选择" lay-reqText="不能为空" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>结束日期：</label>
                        <div class="layuik-input-inline">
                            <input type="text" name="end_date" id="end-date" required lay-verify="required" placeholder="请选择" lay-reqText="不能为空" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>

                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>正副情况：</label>
                        <div class="layuik-input-inline">
                            <select name="chief_value" lay-filter="class_teacherList" data-role="class_teacherList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="opt-area">
                    <a href="javascript:history.go(-1);" class="layui-btn layui-btn-primary back-btn">返回</a>
                    <button class="layui-btn layui-btn-primary" type="button"  id="save-btn" style="color:#2d8cf0;">保存</button>
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="sub">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>

@endsection
